<template>
	<a-form :ref="'ruleForm'" :model="formState" :label-col="{ span: 4 }" :wrapper-col="{ span: 20 }">
		<a-collapse ghost v-model:activeKey="activeKey">
			<a-collapse-panel key="1" header="数据配置">
				<a-row :gutter="[10, 10]">
					<a-col :span="24">
						<a-radio-group
							v-model:value="formState.dataSetConfig.dataSetType"
							optionType="button"
							buttonStyle="solid"
							:options="[
								{ label: '静态', value: 0 },
								{ label: '数据源', value: 1 }
							]"
						>
						</a-radio-group>
					</a-col>
					<a-col :span="24" v-if="formState.dataSetConfig.dataSetType == 0">
						<JsonEditor v-model="formState.data" />
					</a-col>
					<template v-else>
						<a-col :span="24">
							<SelectTree @onChange="selectTreeChange" v-model:paramsJson="formState.dataSetConfig.dataSetJson" />
						</a-col>
						<a-col :span="24">
							<a-form-item label="X轴字段" class="form-item">
								<a-select
									style="width: 100%"
									placeholder="请选择X轴字段"
									v-model:value="formState.data.xField.field"
									:options="dataOption.map((field) => ({ label: field, value: field }))"
								></a-select>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="Y轴字段" class="form-item">
								<a-select
									style="width: 100%"
									placeholder="请选择Y轴字段"
									v-model:value="formState.data.yField.field"
									:options="dataOption.map((field) => ({ label: field, value: field }))"
								></a-select>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="分组字段" class="form-item">
								<a-select
									style="width: 100%"
									placeholder="请选择分组字段"
									v-model:value="formState.data.seriesField.field"
									:options="dataOption.map((field) => ({ label: field, value: field }))"
								></a-select>
							</a-form-item>
						</a-col>
					</template>
				</a-row>
			</a-collapse-panel>
		</a-collapse>
	</a-form>
</template>

<script setup>
	import config from '../config'
	import { reactive, defineProps, defineEmits, computed, ref, watch } from 'vue'
	import { cloneDeep } from 'lodash-es'
	import { designStore } from '@/views/plugin/dashboard/store'
	import JsonEditor from '@/views/plugin/dashboard/plugin/public/JsonEditor/JsonEditor.vue'
	import SelectTree from '@/views/plugin/dashboard/plugin/public/SelectTree/SelectTree.vue'
	import datasetApi from '@/views/plugin/dashboard/api/datasetApi'
	import { message } from 'ant-design-vue'

	const useDesignStore = designStore()
	const emits = defineEmits(['upDataConfig'])
	const activeKey = ref(['1'])

	const props = defineProps({
		config: {
			type: Object,
			default: () => {
				return cloneDeep(config)
			}
		}
	})

	const formState = reactive({
		data: props.config.data,
		dataSetConfig: props.config.dataSetConfig
	})

	// 数据变化清空下拉
	const selectTreeChange = () => {
		formState.data.xField.field = undefined
		formState.data.yField.field = undefined
		formState.data.seriesField.field = undefined
	}

	// 数据源
	const dataSource = ref([])
	// 模拟接口返回下拉数据
	const dataOption = ref([])

	// 获取下拉数据
	watch(
		() => formState.dataSetConfig.dataSetJson,
		(newVal) => {
			if (props.config.dataSetConfig.dataSetType === 1 && newVal) {
				const list = useDesignStore.dataSource || []
				let datasetId = ''
				let indicatorId = ''
				list.forEach((parent) => {
					parent.config.indicators.forEach((child) => {
						if (child.id === newVal.id) {
							datasetId = parent.id
							indicatorId = child.id
						}
					})
				})
				if (datasetId && indicatorId) {
					// 下拉数据
					datasetApi.querySelectedColumns({ datasetId, indicatorId }).then((res) => {
						dataOption.value = res || []
					})
					datasetApi.preview(newVal).then((res) => {
						dataSource.value = res || []
					})
				}
			}
		},
		{
			immediate: true,
			deep: true
		}
	)

	// 预览数据
	const previewData = () => {
		try {
			if (!formState.data.xField.field) {
				message.warning('请选择X轴字段')
				return
			}
			if (!formState.data.yField.field) {
				message.warning('请选择Y轴字段')
				return
			}
			if (!formState.data.seriesField.field) {
				message.warning('请选择分组字段')
				return
			}

			if (dataSource.value && dataSource.value.length > 0) {
				// 处理数据
				formState.data.chartData.value = dataSource.value
				message.success('数据预览成功')
			} else {
				message.warning('暂无数据')
			}
		} catch (error) {
			console.log(error, '数据格式错误')
			message.error('数据格式错误')
		}
	}

	// 更新组件内容
	watch(
		() => formState,
		(newVal) => {
			emits('upDataConfig', { ...props.config, ...newVal })
		},
		{
			deep: true
		}
	)
	const onReload = () => {
		const dataSetJson = props.config.dataSetConfig.dataSetJson
		datasetApi.preview(dataSetJson).then((res) => {
			const dataSource = res || []
			console.log(dataSource)
		})
	}
	defineExpose({ onReload })
</script>

<style lang="less" scoped>
	:deep(.ant-radio-button-wrapper) {
		padding: 0px;
		width: 100%;
	}
	:deep(.ant-radio-group) {
		display: flex;
		text-align: center;
	}
	:deep(.form-item) {
		margin-bottom: 0px;
	}
	.form-item {
		margin-bottom: 0;
	}

	:deep(.ant-radio-button-wrapper) {
		padding: 0px;
		width: 100%;
	}

	:deep(.ant-radio-group) {
		display: flex;
		text-align: center;
	}

	.field-prefix {
		display: flex;
		align-items: center;
	}
</style>
